package ar.fiuba.logger.formatRules;

import ar.fiuba.logger.LogMessageInterface;

/**
 * The Class SeparatorFormatRule.
 * 
 * This class implements the separator format rule. This rule prints the current separator symbol.
 */
public class SeparatorFormatRule implements FormatRuleInterface {
  /**
   * The rule code. This string will be replaced by the rule result.
   */
  private static final String ruleCode = "n";

  /**
   * The separator.
   */
  private String separator;

  /**
   * Instantiates a new separator format rule.
   * 
   * @param separator the separator
   */
  public SeparatorFormatRule(String separator) {
    this.separator = separator;
  }

  /*
   * (non-Javadoc)
   * 
   * @see fiuba.logger.formatRules.FormatRuleInterface#applyFormatRule(java.lang.String)
   */
  @Override
  public String applyFormatRule(String unformattedMessage, LogMessageInterface logMessage) {
    final String regex = "(?<!%)%" + ruleCode;

    return unformattedMessage.replaceAll(regex, separator);
  }

  /*
   * (non-Javadoc)
   * 
   * @see java.lang.Object#equals(java.lang.Object)
   */
  @Override
  public boolean equals(Object obj) {
    if (this == obj)
      return true;
    if (obj == null)
      return false;
    if (getClass() != obj.getClass())
      return false;
    SeparatorFormatRule other = (SeparatorFormatRule) obj;
    if (separator == null) {
      if (other.separator != null)
        return false;
    } else if (!separator.equals(other.separator))
      return false;
    return true;
  }

  /*
   * (non-Javadoc)
   * 
   * @see java.lang.Object#hashCode()
   */
  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((separator == null) ? 0 : separator.hashCode());
    return result;
  }

  /**
   * Sets the separator.
   * 
   * @param separator the new separator
   */
  public void setSeparator(String separator) {
    this.separator = separator;
  }

}
